package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class OrderDaoImpl extends BaseDao implements IOrderDao {
    @Override
    public CutPageBean<OrderBean> cutAll(@RequestParam(value = "pageNO",defaultValue = "1")int pageNO){
        //调用业务方法得到结果
        CutPageBean<OrderBean> cutBean =  new CutPageBean<OrderBean>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select  * from t_order limit ?,?");
            //记录数为页码减一，乘以每页显示条数
            ps.setObject(1,(pageNO-1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);
            rs= ps.executeQuery();
            while (rs.next()){
                OrderBean order = this.getOrder(rs);
                cutBean.getList().add(order);
            }
            ps = con.prepareStatement("select count(*) c from t_order");
            rs =ps.executeQuery();
            if (rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
       return cutBean;
    }

    private OrderBean getOrder(ResultSet rs) throws SQLException {
        OrderBean order = new OrderBean();
        order.setId(rs.getInt("pk_orderId"));
        order.setUserName(rs.getString("o_name"));
        String str = rs.getString("o_orderDate");
        order.setOrderDate(str != null ?LocalDate.parse(str) : null);
        order.setAddress(rs.getString("o_address"));
        return order;
    }

    @Override
    public void add(OrderBean order) {
        this.setConnection();
        try {

            ps=con.prepareStatement("insert into t_order(o_name,o_orderDate,o_address)"+" values (?,?,?)");
            ps.setObject(1,order.getUserName());
            ps.setObject(2,order.getOrderDate());
            ps.setObject(3,order.getAddress());
            ps.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_order where pk_orderId =?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
    }

    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_order set o_address=? where pk_orderId=? ");
            ps.setObject(1,address);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
    }

    @Override
    public OrderBean findById(int id) {
        OrderBean order=null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_order where pk_orderId=? ");
            ps.setObject(1,id);
            //将查询结果，存放至结果查询中
            rs = ps.executeQuery();
            if(rs.next()){
                order = this.getOrder(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return order;
    }
}
